package com.fa.wisdom.ui;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.coder.zzq.smartshow.dialog.LoadingDialog;
import com.fa.wisdom.R;
import com.fa.wisdom.base.BaseActivity;
import com.fa.wisdom.base.Constant;
import com.fa.wisdom.common.App;
import com.fa.wisdom.model.ResultEntity;
import com.fa.wisdom.net.MyCallback;
import com.fa.wisdom.net.Task;
import com.fa.wisdom.utils.Loading;
import com.fa.wisdom.utils.SpUtils;
import com.fa.wisdom.utils.ToastUtils;
import com.tuya.appsdk.sample.resource.HomeModel;
import com.tuya.smart.android.user.api.ILoginCallback;
import com.tuya.smart.android.user.bean.User;
import com.tuya.smart.home.sdk.TuyaHomeSdk;

import retrofit2.Response;

/**
 * create by steven
 * 2021-07-05
 * <p>
 * des:
 */
@SuppressLint("NonConstantResourceId")
public class LoginActivity extends BaseActivity {
    EditText name;
    EditText pwd;

    @Override
    public void setListen() {

    }

    @Override
    protected boolean isStatusBarBlack() {
        return true;
    }

    @Override
    public void setTitleBarColor() {

    }

    public void toRegister(View view) {
        startActivity(new Intent(this, RegisterActivity.class));
    }

    @Override
    public int intiLayout() {
        return R.layout.activity_login;
    }

    LoadingDialog loadingDialog;

    public void login(View v) {
        String n = name.getText().toString();
        String p = pwd.getText().toString();
        if (TextUtils.isEmpty(n) || TextUtils.isEmpty(p)) {
            ToastUtils.showError("账号或密码不能为空！");
            return;
        }
        loadingDialog = Loading.getInstance().onShowLargeLoading("登录中...");
        loadingDialog.showInActivity(this);
        Task.getApiService().login(n, p).enqueue(new MyCallback<ResultEntity>() {
            @Override
            protected void onSuccess(Response<ResultEntity> response) {

                String jsonString = JSON.toJSONString(response.body());
                JSONObject jsonObject = JSON.parseObject(jsonString);
                JSONObject jsonObject1 = jsonObject.getJSONObject("data");
                String token = jsonObject1.getString("token");
                int userID=(jsonObject1.getJSONObject("user_info")).getInteger("id");
                SpUtils.getInstance().encode(Constant.User_id,userID);
                SpUtils.getInstance().encode(Constant.TOKEN, token);
                loginTY();
            }

            @Override
            protected void onFailure(Throwable t) {
                ToastUtils.showError(t.getMessage());
                loadingDialog.dismiss();
            }
        });
    }

    private void loginTY() {
        ILoginCallback callback = new ILoginCallback() {
            @Override
            public void onSuccess(User user) {
                loadingDialog.dismiss();
                ToastUtils.showSuccess("登录成功");
                TuyaHomeSdk.newHomeInstance(Constant.HOME_ID);
                HomeModel.INSTANCE.setCurrentHome(App.getContext(),Constant.HOME_ID);
                startActivity(new Intent(LoginActivity.this, MainActivity.class));
                finish();
            }

            @Override
            public void onError(String code, String error) {
                loadingDialog.dismiss();
            }
        };
        TuyaHomeSdk.getUserInstance().loginWithPhonePassword("86", "13213538793", "123456", callback);
    }

    //获取分类列表进行保存  数据库


    @Override
    public void initView() {
        name = findViewById(R.id.edit_name);
        pwd = findViewById(R.id.edit_pwd);
    }

    @Override
    public void initData() {

    }
}
